﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class GiaoVien : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                NgayBD_Load();                
            }
        }

        protected void Thembtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "GiaoVienThem";
                cmd.Parameters.AddWithValue("@magiaovien", MaGiaoVientxt.Text);
                cmd.Parameters.AddWithValue("@tengiaovien", TenGiaoVientxt.Text);                
                cmd.Parameters.AddWithValue("@ngaysinh", DateTime.Parse(Namddl.SelectedValue + "/" + Thangddl.SelectedValue + "/" + Ngayddl.SelectedValue));
                cmd.Parameters.AddWithValue("@gioitinh", GioiTinhddl.SelectedValue);
                cmd.Parameters.AddWithValue("@hocvi", HocViddl.SelectedValue);
                cmd.Parameters.AddWithValue("@capbac", CapBacddl.SelectedValue);
                cmd.Parameters.AddWithValue("@mabomon", Bomonddl.SelectedValue);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                GiaoViendgv.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Suabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "GiaoVienSua";
                cmd.Parameters.AddWithValue("@magiaovien", MaGiaoVientxt.Text);
                cmd.Parameters.AddWithValue("@tengiaovien", TenGiaoVientxt.Text);
                cmd.Parameters.AddWithValue("@ngaysinh", DateTime.Parse(Namddl.SelectedValue + "/" + Thangddl.SelectedValue + "/" + Ngayddl.SelectedValue));
                cmd.Parameters.AddWithValue("@gioitinh", GioiTinhddl.SelectedValue);
                cmd.Parameters.AddWithValue("@hocvi", HocViddl.SelectedValue);
                cmd.Parameters.AddWithValue("@capbac", CapBacddl.SelectedValue);
                cmd.Parameters.AddWithValue("@mabomon", Bomonddl.SelectedValue);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                GiaoViendgv.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Xoabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "XoaGiaoVien1";
                cmd.Parameters.AddWithValue("@magiaovien", MaGiaoVientxt.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                GiaoViendgv.DataBind();              
                Response.Write("Xóa thành công");
            }
            catch (Exception ex)
            {

                Response.Write("không thực hiện được ");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = GiaoViendgv.SelectedRow;
            MaGiaoVientxt.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenGiaoVientxt.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);            
            Ngayddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[3].Text)).Day.ToString();
            Thangddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[3].Text)).Month.ToString();
            Namddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[3].Text)).Year.ToString();
            GioiTinhddl.SelectedValue = HttpUtility.HtmlDecode(dong.Cells[4].Text);
            HocViddl.SelectedValue = HttpUtility.HtmlDecode(dong.Cells[5].Text);
            CapBacddl.SelectedValue = HttpUtility.HtmlDecode(dong.Cells[6].Text);            
        }
        protected void NgayBD_Load()
        {
            int i;
            for (i = 1950; i < 2030; i++)
            {
                Namddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 13; i++)
            {
                Thangddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 32; i++)
            {
                Ngayddl.Items.Add(i.ToString());
            }
            
        }

        protected void Namddl_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void Delete(string nameTable, string key,string colum)
        {
            SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString);

            //Create Command object
            SqlCommand nonqueryCommand = thisConnection.CreateCommand();

            try
            {
                // Open Connection
                thisConnection.Open();

                // Create INSERT statement with named parameters
                nonqueryCommand.CommandText = "DELETE " + nameTable + " WHERE "+colum+" = @FirstName ";

                

                // Add Parameters to Command Parameters collection
                nonqueryCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar, 20);
                //  nonqueryCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 20);


                nonqueryCommand.Parameters["@FirstName"].Value = key;
                //   nonqueryCommand.Parameters["@LastName"].Value = txtLastName.Text;

                nonqueryCommand.ExecuteNonQuery();
            }

            catch (SqlException ex)
            {
                // Display error
                //   lblErrMsg.Text = ex.ToString();
                // lblErrMsg.Visible = true;
            }

            finally
            {
                // Close Connection
                thisConnection.Close();

            }
            // GridView1.DataBind();
        }
    }
}